<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/transaction/allocation_index.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style//css/transaction/add_form.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/project_device_alert.css">
</script>
<style scoped>
.columnCustomBtn{
    position: relative;
    width: 26px;
    height: 26px;
    padding: 5px;
    line-height: 16px;
    margin-right: 10px;
    text-align: center;
    color: #333;
    border: 1px solid #ccc;
    cursor: pointer;
    -webkit-transition: .5s all;
    transition: .5s all;
}
.btn-inner{
    font-size: inherit;
}
</style>
<div class="layui-fluid">
    <div class="layui-table-title">
        工单详细信息
    </div>
    <div class="layui-table-box-1 layui-form">
        <ul class="layui-row">
            <li class="layui-col-md4 layui-col-sm4">
                <div class="form layui-form-item">
                    <label class="layui-form-label">工单名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="tcReqName" id='tcReqName' class="layui-input" style="border: 0;" disabled>
                    </div>
                </div>
                <div class="form layui-form-item">
                    <label class="layui-form-label">OA申请编号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="fil02" id='fil02' class="layui-input" style="border: 0;" disabled>
                    </div>
                </div>
                <div class="form layui-form-item">
                    <label class="layui-form-label">申请日期：</label>
                    <div class="layui-input-block">
                        <input type="text" name="fil10" id='fil10' class="layui-input" style="border: 0;" disabled>
                    </div>
                </div>
            </li>
            <li class="layui-col-md4 layui-col-sm4">
                <div class="form layui-form-item">
                    <label class="layui-form-label">工单编号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="fil01" id='fil01' class="layui-input" style="border: 0;" disabled>
                    </div>
                </div>
                <div class="form layui-form-item">
                    <label class="layui-form-label">调拨时间：</label>
                    <div class="layui-input-block">
                        <input type="text" name="fil12" id="fil12" placeholder="请选择调拨时间" class="layui-input">
                    </div>
                </div>
                <div class="form layui-form-item ">
                    <label class="layui-label-inlineblock layui-form-label">原所属公司：</label>
                    <div class="layui-input-block tcReqCoorig">
                        <!-- <select id="tcReqCoorig" name="tcReqCoorig" placeholder="请输入原所属公司" lay-search   lay-filter="select1">
                        </select> -->
                        <input type="text" name="tcReqCoorig" id="tcReqCoorig" placeholder="请输入原所属公司" class="layui-input" data="" style="border:0px" readonly>
                    </div>
                </div>
            </li>
            <li class="layui-col-md4 layui-col-sm4">
                <div class="form layui-form-item ">
                    <label class="layui-label-inlineblock layui-form-label">工单状态：</label>
                    <div class="layui-input-block">
                        <input type="text" name="fil05" id='fil05' class="layui-input" style="border: 0;" disabled>
                    </div>
                </div>
                <div class="form layui-form-item ">
                    <label class="layui-label-inlineblock layui-form-label">申请人：</label>
                    <div class="layui-input-block ">
                        <input type="text" name="gen02" id='gen02' class="layui-input" style="border: 0;" disabled>
                    </div>
                </div>
                <div class="form layui-form-item">
                    <label class="layui-label-inlineblock  layui-form-label">拟调往公司：</label>
                    <div class="layui-input-block gen02">
                        <!-- <select id="tcReqCoalt" name="tcReqCoalt" placeholder="请输入拟调往公司" lay-search lay-filter="select2">
                        </select> -->
                        <input type="text" name="tcReqCoalt" id='tcReqCoalt' class="layui-input" placeholder="请输入拟调往公司" data="" style="border:0px" readonly>
                    </div>
                </div>
            </li>
        </ul>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">调拨说明:</label>
            <div class="layui-input-block">
                <textarea name="fil21" id="fil21" class="layui-textarea" ></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="fil07" id="fil07" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="footer layui-btn-containner layui-form-item" style="height: 50px;justify-content: center;margin: 30px 0 0;">
            <button class="hold layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-bc" id="bc">保存</button>
        </div>
    </div>
    <div class="title-btn layui-btn-containner">
        <button class="add layui-btn layui-btn-radius layui-btn-primary">
            <i class="add-icon layui-icon layui-icon-add-circle" style="font-size: 15px;"></i>
            <span class="btn-inner">添加设备</span>
        </button>
        <button class="modify layui-btn layui-btn-radius layui-btn-primary editBatch">
            <i class="add-icon layui-icon layui-icon-edit" style="font-size: 15px;"></i>
            <span class="btn-inner">批量修改</span>
        </button>
        <button class="modify layui-btn layui-btn-radius layui-btn-primary delBatch" style="background-color: #FF4C4C;">
            <span class="btn-inner">批量删除</span>
        </button>
    </div>
    <table class="layui-table" id="LAY-app-content-comm" lay-filter="LAY-app-content-comm"></table>

    <div class="footer layui-btn-containner">
        <button class="commit layui-btn layui-btn-radius layui-btn-primary" lay-submit
            lay-filter="LAY-submit-tj">提交</button>

        <button class="cancel layui-btn layui-btn-radius layui-btn-primary" lay-submit
            lay-filter="LAY-submit-qx">返回</button>
    </div>
</div>

<script type="text/html" id="buttonTpl">
    <div> <span class="table-edit" lay-event="edit"></span>
        <span class="table-delete" lay-event="del"></span></div>
</script>

<!-- 巡检设备选择 -->
<script type="text/html" id="addDeviceTpl">
    <div class="leon-style-alert">
        <div class="layui-alert-box">
            <div class="layui-alert-title">
                <span class="layui-title-inner">设备选择</span>
                <span class="layui-title-screen">
                    <div class="screen-btn">
                        <form class="layui-form" action="">
                            <input type="search" name="keyWord" class="search-blank-inner searchInput"
                                placeholder="搜索" autocomplete="off"/>
                            <button type="button" lay-submit lay-filter="queryTableDeviceHeader"
                                class="search-blank-icon"></button>
                        </form>
                    </div>
                </span>
            </div>
            <div class="layui-content">
                <div class="con1 toggle-btn">
                    <span class="screen">
                        <i class="query-icon  layui-icon"></i>
                        <span class="query-inner">查询条件</span>
                    </span>
                    <i class="icon layui-icon layui-icon-down"></i>
                </div>
                <div class="con2 toggle-content">
                    <form action="" class="layui-form">
                        <label for="">设备编号:</label>
                        <input type="text" name="fia01" />
                        <label for="">设备名称:</label>
                        <input type="text" name="fia02" />
                        <label for="">供应商:</label>
                        <input type="text" name="fia09" />
                        <button type="button" lay-submit lay-filter="queryTableDevice"
                            class="query layui-btn layui-btn-radius layui-btn-primary">
                            <span class="search-icon"></span>
                            <span class="btn-inner">查询</span>
                        </button>
                    </form>
                </div>
                <table class="layui-table" id="table-device-alert"></table>
            </div>
            <div class="footer layui-btn-containner">
                <button class="btn1 layui-btn layui-btn-radius layui-btn-primary">添加</button>
                <button class="btn2 layui-btn layui-btn-radius layui-btn-primary">取消</button>
            </div>
        </div>
    </div>
</script>
<script>
    var cdata = {};
   layui.use(['table', 'admin', 'laypage', 'laytpl', 'tree', 'form', 'api', 'view', 'laydate', 'utill'], function () {
    var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        api = layui.api,
        table = layui.table,
        setter = layui.setter,
        laytpl = layui.laytpl,
        utill = layui.utill,
        tree = layui.tree,
        laydate = layui.laydate,
        form = layui.form;
        $('[data-jump="transaction/balance"]').addClass('layui-this').attr('color', '#FFF');
        form.render(null, 'layuiadmin-form');
        //执行一个laydate实例
        var oldazp01 = ''
        var newazp01 = ''
        function tcReq(data) {
            if(data){
                admin.req({
                url: api.host + api.AzpSelectAllAzp,
                type: "POST",
                done: function (res) {
                    var str = '<option value=""></option>';
                   
                    console.log(data);
                    for (let index = 0; index < res.data.length; index++) {
                        if(data.tcReqCoorig == res.data[index].azp01){
                            console.log("??");
                            str = str + `<option value="${res.data[index].azp01}" selected>${res.data[index].azp02}</option>`
                        }else{
                            str = str + `<option value="${res.data[index].azp01}">${res.data[index].azp02}</option>`
                        }
                       
                    }
                     $('#tcReqCoorig').html(str)
                    form.render('select');
                }
            });
            }
            admin.req({
                url: api.host + api.AzpSelectAllAzp,
                type: "POST",
                done: function (res) {
                    var str = '<option value=""></option>'
                    for (let index = 0; index < res.data.length; index++) {
                         if(data.gen02 == res.data[index].azp01){
                            str = str + `<option value="${res.data[index].azp01}" selected>${res.data[index].azp02}</option>`
                        }else{
                            str = str + `<option value="${res.data[index].azp01}">${res.data[index].azp02}</option>`
                        }
                    }
                    $('#tcReqCoalt').html(str)
                   // $('#tcReqCoorig').html(str)
                    form.render('select');
                }
            });
        }
        admin.req({
            url: api.host + api.FilFileDBSelectFilFileDetails,
            data: {
                id: layui.router().search.id
            },
            done: function (res) {
                var text = ''
                if (res.data.fil05 == 0) {
                    text = '未提交'
                } else if (res.data.fil05 == 1) {
                    text = '待审核'
                } else if (res.data.fil05 == 6) {
                    text = 'OA送审中'
                }
                $('#fil05').val(text)
                //执行一个laydate实例

                laydate.render({
                    elem: '#fil12',
                    value: res.data.fil12,
                    type: 'datetime',
                    format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
                });
                // 玩
                newazp01 = res.data.newazp01
                oldazp01 = res.data.oldazp01
                $('#fil07').val(res.data.fil07)
                $('#fil21').val(res.data.fil21)
                $('#fil01').val(res.data.fil01)
                $('#fil02').val(res.data.fil02)
                $('#tcReqCoorig').val(res.data.oldazp) //2
                $('#tcReqCoorig').attr("data",res.data.oldazp01);

                $('#fil21').val(res.data.fil21)
                $('#tcReqName').val(res.data.tcReqName)
                $('#fil10').val(res.data.fil10)
                $('#gen02').val(res.data.gen02)  
                $('#tcReqCoalt').val(res.data.newazp)  //1
                $('#tcReqCoalt').attr("data",res.data.newazp01);
                tcReq({
                    gen02:res.data.newazp01,
                    tcReqCoorig:res.data.oldazp01,
                    oldazp:res.data.oldazp,
                    newazp:res.data.newazp,
                    tcReqCoalt:res.data.newazp01
                });
            }
        });
        form.on('submit(LAY-submit-bc)', function (data) {
            var field = data.field
            delete field.fil02
            delete field.tcReqName
            delete field.fil05
            delete field.newazp
            delete field.fil10
            var tcReqCoorig = $("#tcReqCoorig").attr('data');
            var tcReqCoalt = $("#tcReqCoalt").attr('data');
            field.tcReqCoorig=tcReqCoorig;
            field.tcReqCoalt=tcReqCoalt;
            if(tcReqCoorig == tcReqCoalt){
               layer.open({content:"原公司与拟调往公司不能相同"})
               return;
            }
            admin.req({
                url: api.host + api.filupdateFilDB,
                type: "post",
                data: JSON.stringify(field),
                dataType: 'json',
                contentType: 'application/json',
                success: function (res) {
                    layer.open({content:res.message})
                }
            });

        })
        form.on('select(select1)', function (data) {
            var tablecache=table.cache['#LAY-app-content-comm'];
            $("#tcReqCoorig").attr("disabled","disabled");
            console.log(tablecache);
        })
        form.on('select(select2)', function (data) {
           console.log("weixuanzhe2");

        })
        form.on('submit(LAY-submit-qx)', function (data) {
            location.hash = '/transaction/balance'
        })

        form.on('submit(LAY-submit-tj)', function (data) {
            
            var field = data.field
            var data = {
                fil01: layui.router().search.fil01,
                tcReqCoorig:$("#tcReqCoorig").attr('data'),
                tcReqCoalt:$("#tcReqCoalt").attr('data'),
                fil12:$("#fil12").val(),
                fil07:$("#fil07").val(),
                fil21:$("#fil21").val(),
                tcReqName:$("#tcReqName").val(),
                fil01:$("#fil01").val()
            }
            if(data.tcReqName==""){
                layer.open({content:"请填写工单名称"})
                return
            }
            if(data.fil12==""){
                layer.open({content:"请选择调拨时间"})
                return
            }
            if(data.tcReqCoalt==""){
                layer.open({content:"请选择拟调往公司"})
                return
            }
            if(data.tcReqCoorig==""){
                layer.open({content:"请选择原公司"})
                return
            }
            if(data.fil07==""){
                layer.open({content:"请填写备注"})
                return
            }
            if(data.fil21==""){
                layer.open({content:"请填写调拨说明"})
                return
            }
            // console.log(data)
            // console.log("+++++++++++")
                admin.req({
                url: api.host + api.filupdateFilDB,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                done: function (res) {
            //var index = layer.load(2);
            utill.loading.show()
            admin.req({
                url: api.host + api.FilFileDBUpdateFileMove2,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                success: function (res) {
                    console.log(res)
                    if(res.code==0){
                        //layer.close(index);
                        utill.loading.hide()
                        layer.msg(res.message)
                        setTimeout(function(){
                            location.hash = '/transaction/balance'
                        },1000)
                    }else{
                          utill.loading.hide()
                        //layer.close(index);
                        layer.open({content:res.message})
                       
                    }
                   
                }
            });
     }
            });

        })

        rander()
        function rander() {
            utill.columnCustom('#LAY-app-content-comm', "FilFileFiaListVO", {
            url: api.host + api.selectDBFiaList,
            method: 'POST',
            where: {
                fil01:layui.router().search.fil01
            },
            toolbar: {
                    toolbar: '#buttonTpl',
                    title: '操作',
                    align: 'center'
                },
        });
            table.on('tool(LAY-app-content-comm)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var data = {
                        fil01: layui.router().search.fil01,
                        fia01: obj.data.tcDetailFia01
                    }
                    layer.confirm('确定要删除该设备吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        admin.req({
                            url: api.host + api.fildeleteDevDB,
                            data: JSON.stringify(data),
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            done: function (res) {
                                layui.table.reload('#LAY-app-content-comm'); //重载表格
                                layer.closeAll()
                            }
                        });
                    }, function () {
                        layer.close();
                    });
                    form.on('submit(LAY-LAY-cancel)',function(data){
                        layer.close(index)
                    });

                } else if (obj.event === 'edit') {
                    cdata = obj.data;
                    localStorage.setItem('fia01', obj.data.tcDetailFia01)
                    localStorage.setItem('tcReqCoalt', newazp01)
                    admin.popup({
                        title: '编辑设备信息',
                        area: ['800px', '700px'],
                        id: 'LAY-popup-content-tags',
                        success: function (layero, index) {
                            view(this.id).render('transaction/branch-one/from')
                                .done(function (data) {
                                    $('.layui-form').on('click','.cancel',function(){
                                        form.render();
                                        layer.closeAll();
                                    })
                                    //监听提交
                                    form.on('submit(LAY-submit)', function (data) {
                                        var field = data.field; //获取提交的字段
                                        var data = {};
                                        
                                        data.tcDetailFil01 = layui.router().search.fil01;
                                        data.tcDetailFia01 = field.fia01;
                                        data.tcDetailFia02 = field.fia02;
                                        // 你调往公司
                                        data.tcDetailFia11 = field.tcDetailFia11.split("/")[1].replace(/\s+/g, "");
                                        data.tcDetailFia10 = field.tcDetailFia10.split("/")[0].replace(/\s+/g, "")
                                        data.tcDetailFia14 = field.tcDetailFia14.split("/")[0].replace(/\s+/g, "")
                                        data.tcDetailFia16 = field.tcDetailFia16.split("/")[0].replace(/\s+/g, "").replace(/^\s+|\s+$/g, '')
                                        data.tcDetailFia15 = field.tcDetailFia15.split("/")[0].replace(/\s+/g, "").replace(/^\s+|\s+$/g, '')
                                        data.tcDetailFia17 = field.tcDetailFia17.split("/")[0].replace(/\s+/g, "")
                                        data.tcDetailFia011 = field.tcDetailFia011.replace(/\s+/g, "")
                                        data.tcDetailFiauser = field.tcDetailFiauser.split("/")[0].replace(/\s+/g,"")

                                        var param = JSON.stringify(data)
                                        admin.req({
                                            url: api.host + api
                                                .filupdateDevDB,
                                            dataType: 'json',
                                            type: "POST",
                                            contentType: 'application/json',
                                            data: param,
                                            done: function (res) {
                                                layui.table.reload('#LAY-app-content-comm'); //重载表格
                                                layer.closeAll()
                                            }
                                        });
                                    });
                                });
                        }
                    });
                }
            });

        }
        

        $('.title-btn').on('click', '.add', function (e) {
            // 表
            var tcReqCoorig = $("#tcReqCoorig").val();
            var tcReqCoalt = $("#tcReqCoalt").val();
            if(tcReqCoorig == tcReqCoalt){
               layer.open({content:"原公司与拟调往公司不能相同"})
            }else{
              layer.open({
                type: 1,
                title: false,
                area: ['662px', '536px'],
                fixed: true,
                shadeClose: true,
                closeBtn: 0,
                content: $('#addDeviceTpl').html(),
                success: alertEvent.addDevice
            }); 
            }
        })
        $('.editBatch').click(function () {
            var checked = table.checkStatus('#LAY-app-content-comm').data;
            let azp01 = newazp01;
            localStorage.setItem('tcReqCoalt',azp01)
            if (checked.length == 0) {
                parent.layer.msg('请至少选择一条数据', { icon: 2 });
                return
            }
            let fia01s = [];
            checked.forEach(elem=>{
                fia01s.push(elem.tcDetailFia01);
            })
            admin.popup({
                title: '批量修改设备信息',
                area: ['400px', '400px'],
                id: 'LAY-popup-content-tags',
                success: function (layero,index){
                    view(this.id).render('transaction/branch-one/eait').done(function () {
                        form.render(null, 'layuiadmin-form');
                        form.on('submit(LAY-submit)', function (data) {
                            var field = data.field; //获取提交的字段
                            field.fia01s = fia01s,
                            field.fil01 = layui.router().search.fil01,
                            field.tcDetailFia11 = field.tcDetailFia11.split(
                                "/")[1]
                            field.tcDetailFia14 = field.tcDetailFia14.split(
                                "/")[0]
                            field.tcDetailFia16 = field.tcDetailFia16.split(
                                "/")[0]
                            field.tcDetailFia15 = field.tcDetailFia15.split(
                                "/")[0]
                            field.tcDetailFia17 = field.tcDetailFia17.split(
                                "/")[0]
                                admin.req({
                                    url: api.host + api.batchUpdatFil01,
                                    dataType: 'json',
                                    type: "POST",
                                    method: 'post',
                                    contentType: 'application/json',
                                    data: JSON.stringify(field),
                                    done: function (res) {
                                        layui.table.reload('#LAY-app-content-comm'); //重载表格
                                        layer.closeAll()
                                    }
                                });
                            // layer.close(index); //执行关闭 


                        });
                        //监听提交
                        $('.layui-form').on('click', '.cancel', function () {
                            layer.close(index); //执行关闭 
                        })
                    });
                }
            });
        })
 //批量删除
 $(".delBatch").click(function () {
            var checkStatus = table.checkStatus('#LAY-app-content-comm');
            var arrs = [];
            for (var i = 0; i < checkStatus.data.length; i++) {
                arrs.push(checkStatus.data[i].tcDetailId);
            }
            if (checkStatus.data.length == 0) {
                parent.layer.msg('请至少选择一条数据', { icon: 2 });
                return;
            }
            layer.confirm('确定要删除选中的设备吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        admin.req({
                            url: api.host + api.devdelDevAlt,
                            data: JSON.stringify(arrs),
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            done: function (res) {
                                layui.table.reload('#LAY-app-content-comm'); //重载表格
                                layer.closeAll()
                            }
                        });
                    }, function () {
                        layer.close();
                    });
        })
        var alertEvent = {
            addDevice: function (layero, index) {
                table.render({
                    elem: '#table-device-alert',
                    id: "#table-device-alert",
                    height: 300,
                    loading: true,
                    method: 'POST',
                    skin: 'nob',
                    toolbar: '',
                    headers: {
                        'token': layui.sessionData(setter.tableName).token
                    },
                    cols: [
                        [{
                                type: 'checkbox',
                                width: 20
                            },
                            {
                                field: 'fia01',
                                title: '设备编号'
                            },
                            {
                                field: 'fia02',
                                title: '设备名称'
                            },
                            {
                                field: 'fic02',
                                title: '设备类型'
                            },
                            {
                                field: 'pmc081',
                                title: '供应商'
                            },
                            {
                                field: 'tcSeriesName',
                                title: '设备系列'
                            },
                            {
                                field: 'fii02',
                                title: '设备型号'
                            }
                        ]
                    ],

                    url: api.host + api.FilFileDBSelectFiaFiles,
                    request: {
                        pageName: 'pageNum',
                        limitName: 'pageSize',
                    },
                    page: {
                        layout: ['prev', 'page', 'next', 'skip'],
                        limits: [10, 20, 30, 50],
                        theme: '#1E9FFF'
                    },
                    parseData: function (res) { //res 即为原始返回的数据
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.message, //解析提示文本
                            "count": res.data.total, //解析数据长度
                            "data": res.data.list //解析数据列表
                        };
                    },
                    contentType: 'application/json',
                    where: {
                        tcReqCoorig: $("#tcReqCoorig").attr('data'),
                        fia01: "",
                        fia02: "",
                        fia09: "",
                        keyWord: "",
                    }
                });
                form.on('submit(queryTableProjectHeader)', function (data) {
                    var allData = data.field;
                    table.reload('#table-device-alert', {
                        where: allData,
                    });
                    return false;
                })
                form.on('submit(queryTableDevice)', function (data) {
                    var allData = data.field;
                    table.reload('#table-device-alert', {
                        where: allData,
                    });
                    return false;
                })
                $('.footer').on('click', '.btn2', function (e) {
                    layer.closeAll()
                })
                $('.footer').on('click', '.btn1', function (e) {
                        var checkedArr = table.checkStatus('#table-device-alert').data;
                        layui.router().search.fil01
                        var arr = []
                        for (let index = 0; index < checkedArr.length; index++) {
                            arr.push({
                                fia01: checkedArr[index].fia01,
                                fil01: layui.router().search.fil01
                            })
                        }
                        admin.req({
                            url: api.host + api.filinsertDevDB,
                            type: "post",
                            contentType: 'application/json',
                            dataType: 'json',
                            data: JSON.stringify(arr),
                            done: function (res) {
                                rander()
                                layer.closeAll()
                            }
                        });
                });
            }
        }



    });
</script>